package cn.iocoder.an.module.business.controller.admin.partlist;

import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.controller.admin.storeinit.vo.DrawStoreInitImportVo;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.partlist.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.partlist.PartListDO;
import cn.iocoder.an.module.business.service.partlist.PartListService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 零件号")
@RestController
@RequestMapping("/business/part-list")
@Validated
public class PartListController {

    @Resource
    private PartListService partListService;

    @PostMapping("/create")
    @Operation(summary = "创建零件号")
    @PreAuthorize("@ss.hasPermission('business:part-list:create')")
    public CommonResult<Long> createPartList(@Valid @RequestBody PartListSaveReqVO createReqVO) {
        return success(partListService.createPartList(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新零件号")
    @PreAuthorize("@ss.hasPermission('business:part-list:update')")
    public CommonResult<Boolean> updatePartList(@Valid @RequestBody PartListSaveReqVO updateReqVO) {
        partListService.updatePartList(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除零件号")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:part-list:delete')")
    public CommonResult<Boolean> deletePartList(@RequestParam("id") Long id) {
        partListService.deletePartList(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除零件号")
                @PreAuthorize("@ss.hasPermission('business:part-list:delete')")
    public CommonResult<Boolean> deletePartListList(@RequestParam("ids") List<Long> ids) {
        partListService.deletePartListListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得零件号")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:part-list:query')")
    public CommonResult<PartListRespVO> getPartList(@RequestParam("id") Long id) {
        PartListDO partList = partListService.getPartList(id);
        return success(BeanUtils.toBean(partList, PartListRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得零件号分页")
    @PreAuthorize("@ss.hasPermission('business:part-list:query')")
    public CommonResult<PageResult<PartListRespVO>> getPartListPage(@Valid PartListPageReqVO pageReqVO) {
        PageResult<PartListDO> pageResult = partListService.getPartListPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PartListRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出零件号 Excel")
    @PreAuthorize("@ss.hasPermission('business:part-list:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPartListExcel(@Valid PartListPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PartListDO> list = partListService.getPartListPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "零件号.xls", "数据", PartListRespVO.class,
                        BeanUtils.toBean(list, PartListRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<PartListImportVo> list = Arrays.asList(
                PartListImportVo.builder().partNumber("6396453").supplyCode("SDL0032").supplyName("宁波福士汽车部件有限公司").partDes("增压器回油管").build(),
                PartListImportVo.builder().partNumber("6456149").supplyCode("EGR出口管").supplyName("宁波福士汽车部件有限公司").partDes("增压器回油管").build(),
                PartListImportVo.builder().partNumber("3695681").supplyCode("SDL0032").supplyName("宁波福士汽车部件有限公司").partDes("机油输油管").build()
                );
        // 输出
        ExcelUtils.write(response, "导入模板.xlsx", "列表", PartListImportVo.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<PartListImportVo> list = ExcelUtils.read(file, PartListImportVo.class);
        return success(partListService.importList(list, updateSupport));
    }


}